package cn.com.common.job.task;


import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.quartz.DisallowConcurrentExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.com.common.constants.Constants;
import cn.com.gas.entity.GasForewarningZeroEntity;
import cn.com.gas.entity.GasStationDeptEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.entity.GasStaytimeResultEntity;
import cn.com.gas.service.GasForewarningZeroService;
import cn.com.gas.service.GasStationDeptService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.gas.service.GasStaytimeResultService;
import cn.com.util.TsBqUtils;

/**
 * 定时任务(演示Demo，可删除) testTask为spring bean的名称， 方法名称必须是run
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Component("gasForewarningZeroProcessTask")
@DisallowConcurrentExecution
// @Transactional
public class GasForewarningZeroProcessTask {

	@Value("${gasstation.timedTask.gasForewarningZeroProcessTask}")
	private String gasForewarningZeroProcessTask;

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private GasStationDeptService gasStationDeptService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	@Autowired
	private GasForewarningZeroService gasForewarningZeroService;

	@Autowired
	private GasStaytimeResultService gasStaytimeResultService;

	public void run(String params) throws Exception {

		logger.debug("application参数为gasForewarningZeroProcessTask：{}", gasForewarningZeroProcessTask);

		if (gasForewarningZeroProcessTask.equals(Constants.JOB_STOP)) {
			logger.debug("application配置文件控制不执行gasForewarningZeroProcessTask定时任务");
			throw new Exception("application配置文件控制不执行gasForewarningZeroProcessTask定时任务");
		}

		List<GasStationDeptEntity> stationDeptList = gasStationDeptService.list();
		List<String> deptIdList = new ArrayList<>();
		for (GasStationDeptEntity stationDeptEntity : stationDeptList) {
			String deptId = stationDeptEntity.getDeptId();
			deptIdList.add(deptId);

		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		// 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 减去2小时
        LocalDateTime hoursAgo = now.minusHours(2);
        
        String nowStr = now.format(formatter);
        String hoursAgoStr = hoursAgo.format(formatter);
		
		List<String> _deptIdList = deptIdList.stream().distinct().collect(Collectors.toList());

		for (String _deptId : _deptIdList) {

			LambdaQueryWrapper<GasStationinfoEntity> queryWrapperGasStationinfo = Wrappers.lambdaQuery();
			queryWrapperGasStationinfo.eq(GasStationinfoEntity::getType, "group");
			queryWrapperGasStationinfo.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			queryWrapperGasStationinfo.eq(GasStationinfoEntity::getTaxorgcode, _deptId);
			
			List<GasStationinfoEntity> gasStationinfoList = gasStationinfoService.list(queryWrapperGasStationinfo);
			List<String> gasStationinfoIdList = new ArrayList<>();
			
			for (GasStationinfoEntity gasStationinfoEntity : gasStationinfoList) {
				gasStationinfoIdList.add(gasStationinfoEntity.getId());
			}
			
			if(TsBqUtils.isNotNull(gasStationinfoIdList)){
				
				LambdaQueryWrapper<GasStaytimeResultEntity> gasStaytimeResultqueryWrapper = Wrappers.lambdaQuery();

				gasStaytimeResultqueryWrapper.in(GasStaytimeResultEntity::getStationId, gasStationinfoIdList);
				
				gasStaytimeResultqueryWrapper.ge(GasStaytimeResultEntity::getMessageDisappear, sdf.parse(hoursAgoStr));
				gasStaytimeResultqueryWrapper.le(GasStaytimeResultEntity::getMessageDisappear, sdf.parse(nowStr));

				int count = gasStaytimeResultService.count(gasStaytimeResultqueryWrapper);
				
				logger.debug("税务机关代码：" + _deptId + "_时间："+ nowStr + "至" + hoursAgoStr + "_数量：" +count);
				
				if(count == 0){
					
					GasForewarningZeroEntity entity = new GasForewarningZeroEntity();
					entity.setTaxorgcode(_deptId);
					entity.setZeroTime(nowStr + "至" + hoursAgoStr);
					entity.setDeleted(Constants.DATA_NOT_DELETED);
					entity.setCreateTime(date);
					gasForewarningZeroService.save(entity);
					
				}
				
			}
			
		}

	}

}
